package cn.remex.mongo;

import cn.remex.db.DbCvo;
import cn.remex.db.DbCvoBase;
import cn.remex.db.sql.Where;
import cn.remex.db.sql.WhereRuleOper;
import com.mongodb.client.MongoCollection;
import org.bson.Document;

/**
 * 名称：
 * 缩写：
 * 用途：
 * Created by yangy on 2017/1/14 0014.
 */
public class MongoCvo extends DbCvoBase {

	private final MongoCollection<Document> mongoCollection;

	public MongoCvo(MongoCollection<Document> mongoCollection) {
		this.mongoCollection = mongoCollection;
	}

	public MongoRvo execute(){
		return new MongoRvo(mongoCollection.find()
			.skip(this.rowCount*(pagination-1)).limit(rowCount)
		);
	}

	public MongoCvo filter(Where filter){
		this.setFilter(filter);
		return this;
	}
	public MongoCvo filterBy(final String field, final WhereRuleOper ruleOper, final Object value){
		this.addRule(field, ruleOper, value);
		return this;
	}

	//====page control======================//
	//分页函数
	public MongoCvo page(int page){
		setPagination(page);
		return this;
	}
	public MongoCvo rowCount(int rowCount){
		setRowCount(rowCount);
		return this;
	}

	public MongoCollection<Document> getMongoCollection() {
		return mongoCollection;
	}
}
